<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="{{url_for('static',filename ='css/font.css')}}">
    <link rel="stylesheet" href="{{url_for('static',filename ='css/xadmin.css')}}">
    <script type="text/javascript" src="{{url_for('static',filename ='js/jquery-3.4.1.min.js')}}"></script>
    <script src="{{url_for('static',filename ='lib/layui/layui.js')}}" charset="utf-8"></script>
    <script type="text/javascript" src="{{url_for('static',filename ='js/xadmin.js')}}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<body class="layui-anim layui-anim-up">
<div class="x-nav">
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right"
       href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">ဂ</i>
    </a>
</div>
<div class="x-body">
    <table id="UserInfo" lay-filter="UserInfo"></table>
</div>
</body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<script>

    // <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>

    province = ''
    city = ''
    layui.use(['table'], function () {
        var table = layui.table;
        var select = layui.select;
        var tableIns = table.render({
            elem: '#UserInfo'  //绑定table表格
            , id: 'UserInfo'
            , method: 'post'
            , url: '{{url_for("manger.selectallUserInfo")}}' //后台springmvc接收路径
            , page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                //,curr: 5 //设定初始在第 5 页
                , groups: 1 //只显示 1 个连续页码
                , first: false //不显示首页
                , last: false //不显示尾页
                , limit: 7
                , limits: [7, 14, 21]
            }
            , cols: [
                [
                    {field: 'id', title: 'id号', sort: true}
                    , {field: 'username', title: '用户名'}
                    , {field: 'email', title: '邮箱'}
                    , {field: 'password_hash', title: '密码'}
                    , {field: 'operation', title: '操作', toolbar: '#barDemo'}
                ]
            ]
        });

        // 监听工具条事件（放在 table.render 之后）
        table.on('tool(UserInfo)', function (obj) {
            var data = obj.data;

            if (obj.event === 'delete') {
                layer.confirm(
                    '确定删除用户 ' + data.username + ' 吗？',
                    {
                        btn: ['确定', '取消']
                    },
                    function (index) { // ✅ 使用回调参数 index
                        $.ajax({
                            url: '{{url_for("manger.deleteUser")}}',  // 注意拼写 manger → manager
                            method: 'POST',
                            data: {id: data.id},
                            success: function (res) {
                                if (res.success) {
                                    obj.del();
                                    layer.msg('删除成功', {icon: 1});
                                } else {
                                    layer.msg('失败：' + (res.message || '未知错误'), {icon: 2});
                                }
                            },
                            error: function (xhr) {
                                layer.msg('请求失败：' + xhr.statusText, {icon: 2});
                            }
                            // 移除 complete 中的关闭逻辑
                        });
                    }
                );
            }
        });

    })
</script>
</html>
